package com.ktjy.actual.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;
@Aspect
@Component
public class PrintAspect {
    private static final Logger logger= LoggerFactory.getLogger(PrintAspect.class);
//    前置增强
    @Pointcut("execution(* com.ktjy.actual.service.PrintService.print(com.ktjy.actual.entity.Paper))")
    public void pt(){}
    @Before(value = "pt()")
    public void before(JoinPoint jp){
        logger.info("[PrintAspect 前置增强] 正在调用{}的{}方法. 方法入参:{}",jp.getTarget(),jp.getSignature().getName(), Arrays.toString(jp.getArgs()));
    }
    //后置增强
    @AfterReturning(value = "pt()",returning = "result")
    public void afterReturning(JoinPoint jp,Object result){
        logger.info("[PrintAspect 后置增强] {}的{}方法正常返回. 返回结果:{}",jp.getTarget(),jp.getSignature().getName(),result);
    }
    //异常增强
    @AfterThrowing(value = "pt()",throwing = "e")
    public void afterThrowing(JoinPoint jp,Throwable e){
        logger.info("[PrintAspect 异常增强] {}的{}方法异常. 异常信息:{}",jp.getTarget(),jp.getSignature().getName(),e.getMessage());
    }
    //最终增强
    @After(value = "pt()")
    public void after(JoinPoint jp){
        logger.info("程序结束");
    }
}